package cate.game.role.res.findback.impl;

import cate.common.table.d.GD;
import cate.common.table.d.GDAdventure;
import cate.common.table.d.GDFunc;
import cate.common.table.farm.adventure.row.AdventureCharpterRow;
import cate.common.table.findback.base.FindBackBaseRow;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.role.res.findback.po.RoleFindBackFuncPO;
import easy.java.dev.note.NoteClass;

import java.util.Iterator;
import java.util.Random;

@NoteClass("资源找回-快速作战")
public class RoleFindBackQuickFightPO extends RoleFindBackFuncPO {
    @Override
    public int getFuncId() {
        return GDFunc.QUICK_FIGHT;
    }

    @Override
    public void calcFindBack(boolean skipDay) {
        FindBackBaseRow config = getConfig();
        if (config == null) {
            return;
        }
        // 防止玩家一关没打点击了快速补给，此时次数减少但没有发送奖励
        AdventureCharpterRow row = role.getFarm().adventure.getRow();
        if (row == null) {
            return;
        }

        int findBackTimes = skipDay ?
                role.getFarm().adventure.playTimes.getFreeTotal(role, GDFunc.QUICK_FIGHT)
                : role.getFarm().adventure.playTimes.getFreeRemain(role, GDFunc.QUICK_FIGHT);
        if (findBackTimes > 0) {
            MixRes res = calcQuickFightReward();
            if (!res.isEmpty()) {
                // 免费
                this.times = findBackTimes;
                this.freeReward = res.copy();
                Iterator<MixResItem> iter = freeReward.items.iterator();
                while (iter.hasNext()) {
                    MixResItem item = iter.next();
                    if (item.num > 1) {
                        item.multiplyDouble(config.rate / GD.W_10000_0);
                    } else {
                        if (new Random().nextInt(10000) >= config.rate) {
                            iter.remove();
                        }
                    }
                }
                freeReward.items.removeIf(item -> item.num <= 0);
                this.cost = new MixRes(config.costStr);
                this.payReward = res.copy();
            }
        }
    }

    /**
     * 计算快速作战奖励
     */
    private MixRes calcQuickFightReward() {
        MixRes res = role.getFarm().adventure.buildBaseReward(GDAdventure.REWARD_TIME);
        res.addList(role.getFarm().adventure.randomDrop.getQuickFightReward(GDAdventure.REWARD_TIME, GDFunc.QUICK_FIGHT));
        return res;
    }
}
